package com.project.code.luck.controller;

import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import com.project.common.annotation.Log;
import com.project.common.core.template.BaseController;
import com.project.common.core.template.Result;
import javax.annotation.Resource;

import com.project.common.utils.bean.CopyObjectUtils;
import com.project.common.enums.BusinessType;
import com.project.code.luck.entity.po.LuckUser;
import com.project.code.luck.entity.params.LuckUserAddParams;
import com.project.code.luck.entity.params.LuckUserEditParams;
import com.project.code.luck.entity.params.LuckUserPageParams;
import com.project.code.luck.entity.export.LuckUserExport;
import com.project.code.luck.entity.vo.LuckUserVo;
import com.project.code.luck.service.LuckUserService;
import com.project.common.utils.excel.ExcelUtil;
import com.project.common.core.page.Pager;

/**
 * 好运用户 Controller
 *
 * @author WangYan
 * @date 2023-05-14
 */
@Api(tags = "好运用户")
@RestController
@RequestMapping("/luck/user")
public class LuckUserController extends BaseController {

    @Resource
    private LuckUserService luckUserService;

    /**
     * http://localhost:8085/luck/user/oom
     * vm 启动参数：-Xms200m -Xmx200m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=F:\dump\oom_dump.hprof
     */
    @ApiOperation(value = "edit")
    @Log(title = "oom", businessType = BusinessType.UPDATE)
    @GetMapping("/oom")
    public Result oom() {
        luckUserService.oom();
        return Result.success();
    }

    @ApiOperation(value = "add")
//    @PreAuthorize("@ss.hasPermi('luck:user:add')")
    @Log(title = "好运用户", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody LuckUserAddParams params) {
        return Result.success(luckUserService.insert(CopyObjectUtils.copyAtoB(params,LuckUser.class)));
    }

    @ApiOperation(value = "edit")
    @PreAuthorize("@ss.hasPermi('luck:user:edit')")
    @Log(title = "好运用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody @Valid LuckUserEditParams params) {
        return Result.success(luckUserService.updateById(CopyObjectUtils.copyAtoB(params,LuckUser.class)));
    }

    @ApiOperation(value = "delete")
    @PreAuthorize("@ss.hasPermi('luck:user:remove')")
    @Log(title = "好运用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Long[] ids) {
        return Result.success(luckUserService.delete(Arrays.asList(ids)));
    }

    @ApiOperation(value = "read")
    @PreAuthorize("@ss.hasPermi('luck:user:query')")
    @GetMapping(value = "/{id}")
    public Result<LuckUserVo> read(@PathVariable("id") Long id) {
        return Result.success(luckUserService.read(id, LuckUserVo.class));
    }

    @ApiOperation(value = "list")
    @PreAuthorize("@ss.hasPermi('luck:user:list')")
    @GetMapping("/list")
    public Pager<LuckUserVo> list(LuckUserPageParams params) {
        startPage();
        List<LuckUserVo> result = luckUserService.list(params, LuckUserVo.class);
        return toPage(result);
    }

    @ApiOperation(value = "export")
    @PreAuthorize("@ss.hasPermi('luck:user:export')")
    @Log(title = "好运用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(LuckUserPageParams params) {
        List<LuckUserExport> list = luckUserService.list(params, LuckUserExport.class);
        ExcelUtil<LuckUserExport> util = new ExcelUtil<LuckUserExport>(LuckUserExport.class);
        return util.exportExcel(list, "user");
    }

}
